<template>
	<view class="summaryGeneration_box">
		<view class="content">
			<view class="textarea">
				<u-textarea height="498rpx" maxlength="3000" v-model="abstract" border="false"
					placeholder="点击输入文案或长按粘贴文案">
				</u-textarea>
			</view>
			<view class="tools">
				<view class="tools_left">
					<view class="left" @click="delAbstract">
						<u-icon name="trash" size="18" color="#999999"></u-icon>
						<text class="text">清除文本</text>
					</view>
				</view>
				<view class="tools_right">
					<text class="text">字数 {{abstract.length}}</text>
				</view>
			</view>
		</view>
		<view class="button">
			<view class="button_left">
				<u-button text="复制文本" color="#5490FF" size="large" @click="copyAbstract"></u-button>
			</view>
			<view class="button_right">
				<u-button text="生成摘要" color="#1EB930" size="large" @click="generateAbstract"></u-button>
			</view>
		</view>
		<view class="abstract_results" v-if="abstractResults.length!=0">
			<view class="title">
				摘要结果
			</view>
			<view class="results_textarea" style="padding: 0 20rpx 20rpx 20rpx;">
				<!-- <u-textarea autoHeight maxlength="-1" v-model="abstractResults" border="false" placeholder="摘要结果">
				</u-textarea> -->
				<text user-select='true' style="color: #606266;font-size: 15px;">{{abstractResults}}</text>
			</view>
			<view class="abstract_results_btn" @click="copyAbstractResults">
				<u-button text="复制结果" color="#5490FF" size="large"></u-button>
			</view>
		</view>
		<!-- 非会员看广告提示 -->
		<u-modal :show="vipShow" :title="'温馨提示'" :content="content" cancelText="取消" confirmText="去开通"
			showCancelButton="true" @cancel="cancel" @confirm="govip" :closeOnClickOverlay="true"
			@close="vipShow = false"></u-modal>
	</view>
</template>

<script>
	import {
		wxTextDetection
	} from "../../utils/textCheck.js";
	import {
		apiMixUrlApi
	} from '@/api/assemb.js'
	export default {
		data() {
			return {
				vipShow: false,
				isVip: false,
				content: '您还不是会员,此功能需开通会员才可使用。',
				abstract: '',
				abstractResults: '',
				accessToken: ''
			};
		},
		onShow() {
			this.isVip = uni.getStorageSync('vipType').vipStatus == 1 ? true : false;
			// this.isVip = true;
		},
		onLoad() {
			this.accessToken = getApp().globalData.accessToken
		},
		methods: {
			/**
			 * 跳转充值页面
			 */
			govip() {
				this.vipShow = false;
				if (uni.getStorageSync('OS') !== 'ios') {
					uni.navigateTo({
						url: '/pages/paymentCenter/paymentCenter'
					});
				} else {
					uni.showModal({
						title: '提示',
						content: '由于相关规范，IOS功能暂不可用',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},
			cancel() {
				this.vipShow = false;
				// 用户触发广告后，显示激励视频广告
				// if (videoAd) {
				// 	videoAd.show().catch(() => {
				// 		// 失败重试
				// 		videoAd
				// 			.load()
				// 			.then(() => videoAd.show())
				// 			.catch(err => {
				// 				console.log('激励视频 广告显示失败');
				// 			});
				// 	});
				// }
			},
			copyAbstractResults() {
				uni.setClipboardData({
					data: this.abstractResults,
					showToast: true
				});
			},
			generateAbstract() {
				if (this.abstract.indexOf("克强") != -1){
					uni.navigateTo({
						url: '/pages/minGie/minGie'
					})
					this.abstract = ''
					return
				}
				
				if (this.abstract.trim() == '') {
					uni.showToast({
						title: '请输入文案',
						duration: 2000,
						icon: 'none'
					});
				} else if (this.isVip) {
					console.log('生成摘要');
					uni.showLoading({
						title: '检测中...',
						mask: true
					});
					let prams = {
						text: this.abstract.substring(0, 3000)
					}
					apiMixUrlApi(prams, 1089, 'DYTextCheck').then((res) => {
						console.log(res);
						uni.hideLoading()
						if (!res.hit) {
							uni.showLoading({
								title: "加载中...",
								mask: true
							})
							wx.request({
								url: `https://aip.baidubce.com/rpc/2.0/nlp/v1/news_summary?access_token=${this.accessToken}&charset=UTF-8`,
								method: "POST",
								data: {
									content: this.abstract,
									max_summary_len: 200
								},
								success: (res) => {
									console.log(res);
									this.abstractResults = res.data.summary
									uni.hideLoading()
								}
							})
						} else {
							tt.showModal({
								title: "提示",
								content: "检测到敏感关键词，请修改后重试！",
								showCancel: false,
								success(res) {},
							});
						}
					})
				} else {
					this.vipShow = true
				}
			},
			copyAbstract() {
				if (this.abstract.trim() == '') {
					uni.showToast({
						title: '请输入文案',
						duration: 2000,
						icon: 'none'
					});
				} else {
					uni.setClipboardData({
						data: this.abstractResults,
						showToast: true
					});
				}

			},
			delAbstract() {
				this.abstract = ''
			}

		}
	}
</script>

<style lang="scss">
	/deep/.u-textarea--disabled {
		background-color: #fff !important;
	}

	/deep/.u-textarea {
		padding-top: 0 !important;
		padding-bottom: 0 !important;
	}

	/deep/.u-button {
		border-radius: 12rpx !important;
	}

	.title {
		line-height: 111rpx;
		margin-left: 268rpx;
		font-size: 34rpx;
		font-family: PingFang SC;
		color: #5490FF;
	}

	.abstract_results {
		padding-bottom: 30rpx;
		margin-top: 43rpx;
		width: 696rpx;
		background: #FFFFFF;
		border-radius: 18rpx;
		box-sizing: border-box;

		.abstract_results_btn {
			margin: auto;
			margin-top: 30rpx;
			width: 333rpx;
		}

	}

	.button {
		margin-top: 22rpx;
		width: 100%;
		display: flex;
		justify-content: space-between;

		.button_left {
			width: 47%;
		}

		.button_right {
			position: relative;
			width: 47%;
		}
	}

	.textarea {
		height: 498rpx;
	}

	.text {
		font-size: 19rpx;
		font-family: PingFang SC;
		color: #999999;
	}

	.summaryGeneration_box {
		padding: 0 27rpx;

		.content {
			width: 696rpx;
			height: 598rpx;
			border-radius: 18rpx;
			background-color: #fff;
		}

		.tools {
			height: 100rpx;
			padding: 0 27rpx;
			display: flex;
			justify-content: space-between;

			.tools_left {
				display: flex;
				justify-content: space-between;

				.left {
					display: flex;
					justify-content: space-between;
					align-items: center;
				}
			}

			.tools_right {
				display: flex;
				align-items: center;
			}
		}
	}
</style>
<style>
	page {
		background-color: #f4f4f4;
	}
</style>